/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package card.gui;

import bank.gui.JDialog_Deposit;
import bank.gui.JDialog_Withdraw;
import frmwrk.core.FinancialFramework;
import gui.core.JDialog_AbstractArea;
import gui.core.JDialog_AbstractEntry;
import java.awt.event.ActionEvent;
import java.util.Observable;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;


/**
 *
 * @author Damian
 */
public class CardFrm extends gui.core.CoreFrame {

    FinancialFramework ff;
    String ccnumber, expdate;
    
    javax.swing.JButton JButton_NewCCAccount;
    javax.swing.JButton JButton_GenBill;
    javax.swing.JButton JButton_Deposit;
    javax.swing.JButton JButton_Withdraw;
    javax.swing.JButton JButton_Exit;

    public CardFrm(FinancialFramework financialFramework) {
        this.ff = financialFramework;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        Object object = event.getSource();
        if (object == JButton_Exit) {
            JButtonExit_actionPerformed(event);
        } else if (object == JButton_NewCCAccount) {
            JButtonNewCCAC_actionPerformed(event);
        } else if (object == JButton_GenBill) {
            JButtonGenerateBill_actionPerformed(event);
        } else if (object == JButton_Deposit) {
            JButtonDeposit_actionPerformed(event);
        } else if (object == JButton_Withdraw) {
            JButtonWithdraw_actionPerformed(event);
        }
    }

    @Override
    protected JButton[] makeButtons() {
        JButton_NewCCAccount = new javax.swing.JButton();
        JButton_GenBill = new javax.swing.JButton();
        JButton_Deposit = new javax.swing.JButton();
        JButton_Withdraw = new javax.swing.JButton();

        JButton_Exit = new javax.swing.JButton();


        JButton_NewCCAccount.setText("Add Credit-card account");

        JButton_NewCCAccount.setBounds(24, 20, 192, 33);
        JButton_NewCCAccount.setActionCommand("NewCCAccount");

        JButton_GenBill.setText("Generate Monthly bills");
        JButton_GenBill.setActionCommand("CompAC");

        JButton_GenBill.setBounds(240, 20, 192, 33);
        JButton_Deposit.setText("Deposit");

        JButton_Deposit.setBounds(468, 104, 96, 33);
        JButton_Withdraw.setText("Withdraw");



        JButton_Withdraw.setBounds(468, 164, 96, 33);
        JButton_Exit.setText("Exit");

        JButton_Exit.setBounds(468, 248, 96, 33);

        JButton_Exit.addActionListener(this);
        JButton_NewCCAccount.addActionListener(this);
        JButton_GenBill.addActionListener(this);
        JButton_Deposit.addActionListener(this);
        JButton_Withdraw.addActionListener(this);



        return new JButton[]{JButton_NewCCAccount, JButton_GenBill, JButton_Deposit, JButton_Withdraw,
                    JButton_Exit
                };
    }

    @Override
    protected DefaultTableModel makeTable() {
        model = new DefaultTableModel();
        model.addColumn("Name");
        model.addColumn("CC number");
        model.addColumn("Exp date");
        model.addColumn("Type");
        model.addColumn("Balance");
        rowdata = new Object[7];
        newaccount = false;

        return model;
    }

    void JButtonExit_actionPerformed(java.awt.event.ActionEvent event) {
        System.exit(0);
    }

    void JButtonNewCCAC_actionPerformed(java.awt.event.ActionEvent event) {
        /*
        JDialog_AddPAcc type object is for adding personal information
        construct a JDialog_AddPAcc type object
        set the boundaries and show it
         */

        JDialog_AddCCAccount ccac = new JDialog_AddCCAccount(this);
        ccac.setBounds(450, 20, 300, 380);
        ccac.show();

        if (newaccount) {
            // add row to table
            rowdata[0] = clientName;
            rowdata[1] = ccnumber;
            rowdata[2] = expdate;
            rowdata[3] = accountType;
            rowdata[4] = "0";
            model.addRow(rowdata);
            JTable1.getSelectionModel().setAnchorSelectionIndex(-1);
            newaccount = false;
        }



    }

    void JButtonGenerateBill_actionPerformed(java.awt.event.ActionEvent event) {
        JDialog_AbstractArea billFrm = new JDialogGenBill();
        billFrm.setBounds(450, 20, 400, 350);
        billFrm.show();

    }

    void JButtonDeposit_actionPerformed(java.awt.event.ActionEvent event) {
        // get selected name
        int selection = JTable1.getSelectionModel().getMinSelectionIndex();
        if (selection >= 0) {
            String name = (String) model.getValueAt(selection, 0);

            //Show the dialog for adding deposit amount for the current mane
            JDialog_AbstractEntry dep = new JDialog_Deposit(thisframe, name);
            dep.setBounds(430, 15, 275, 140);
            dep.show();

            // compute new amount
            long deposit = Long.parseLong(amountDeposit);
            String samount = (String) model.getValueAt(selection, 4);
            long currentamount = Long.parseLong(samount);
            long newamount = currentamount + deposit;
            model.setValueAt(String.valueOf(newamount), selection, 4);
        }


    }

    void JButtonWithdraw_actionPerformed(java.awt.event.ActionEvent event) {
        // get selected name
        int selection = JTable1.getSelectionModel().getMinSelectionIndex();
        if (selection >= 0) {
            String name = (String) model.getValueAt(selection, 0);

            //Show the dialog for adding withdraw amount for the current mane
            JDialog_AbstractEntry wd = new JDialog_Withdraw(thisframe, name);
            wd.setBounds(430, 15, 275, 140);
            wd.show();

            // compute new amount
            long deposit = Long.parseLong(amountDeposit);
            String samount = (String) model.getValueAt(selection, 4);
            long currentamount = Long.parseLong(samount);
            long newamount = currentamount - deposit;
            model.setValueAt(String.valueOf(newamount), selection, 4);
            if (newamount < 0) {
                JOptionPane.showMessageDialog(JButton_Withdraw, " " + name + " Your balance is negative: $" + String.valueOf(newamount) + " !", "Warning: negative balance", JOptionPane.WARNING_MESSAGE);
            }
        }


    }

    public CardFrm() {
        super.setTitle("Credit-card processing Application.");

        model.addColumn("Name");
        model.addColumn("CC number");
        model.addColumn("Exp date");
        model.addColumn("Type");
        model.addColumn("Balance");
        rowdata = new Object[7];
        newaccount = false;


        JButton_NewCCAccount.setText("Add Credit-card account");
        JPanel1.add(JButton_NewCCAccount);
        JButton_NewCCAccount.setBounds(24, 20, 192, 33);
        JButton_GenBill.setText("Generate Monthly bills");
        JButton_GenBill.setActionCommand("jbutton");
        JPanel1.add(JButton_GenBill);
        JButton_GenBill.setBounds(240, 20, 192, 33);
        JButton_Deposit.setText("Deposit");
        JPanel1.add(JButton_Deposit);
        JButton_Deposit.setBounds(468, 104, 96, 33);
        JButton_Withdraw.setText("Charge");
        JPanel1.add(JButton_Withdraw);
        JButton_Withdraw.setBounds(468, 164, 96, 33);
        JButton_Exit.setText("Exit");
        JPanel1.add(JButton_Exit);
        JButton_Exit.setBounds(468, 248, 96, 31);


        JButton_GenBill.setActionCommand("jbutton");

        SymWindow aSymWindow = new SymWindow();
        this.addWindowListener(aSymWindow);
//        SymAction lSymAction = new SymAction();
//        JButton_Exit.addActionListener(lSymAction);
//        JButton_NewCCAccount.addActionListener(lSymAction);
//        JButton_GenBill.addActionListener(lSymAction);
//        JButton_Deposit.addActionListener(lSymAction);
//        JButton_Withdraw.addActionListener(lSymAction);




    }

    @Override
    public void update(Observable o, Object arg) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
